// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 59 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function138

// Static Values

// Tables

function Eggman_Function138
	Sin256(object.ypos, object.value2)
	object.ypos <<= 10
	object.ypos += object.value1
	object.value2 += 2
	object.value2 &= 255
end function


event ObjectMain
	if object.value14 == 1
		object.value13++
		if object.value13 == 4
			object.value13 = 0
			RotatePalette(0, 194, 196, 0)
			RotatePalette(0, 197, 202, 0)
		end if
	else
		object.value13++
		if object.value13 == 4
			object.value13 = 0
			RotatePalette(0, 112, 114, 0)
			RotatePalette(0, 115, 120, 0)
		end if
	end if
	switch object.state
	case 0
		if object[0].xpos > object[-1].xpos
			temp0 = -32
			temp0 *= screen.xsize
			temp0 /= 320
			temp0 -= 192
			temp0 += object[-1].ixpos
			stage.newXBoundary1 = temp0
			temp0 = 32
			temp0 *= screen.xsize
			temp0 /= 320
			temp0 += 192
			temp0 += object[-1].ixpos
			stage.newXBoundary2 = temp0
			object.value6 = object[-1].xpos
			object.value6 -= 0x800000
			object.value7 = object[-1].xpos
			object.value7 += 0x800000
			object.value8 = object.ypos
			object.value9 = object.ypos
			object.value9 += 0x2C0000
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object[-2].type = TypeName[Eggman Claw]
			object[-2].propertyValue = 0
			object[-2].value1 = object.entityPos
			object[+1].type = TypeName[Eggman Claw]
			object[+1].propertyValue = 1
			object[+1].value1 = object.entityPos
			object[+2].priority = PRIORITY_ACTIVE
			object[+2].state = 1
			object.xvel = -0x18000
			object.value1 = object.ypos
			CheckCurrentStageFolder("Zone04")
			if checkResult == 1
				object.value14 = 1
			else
				object.value14 = 0
			end if
			object.value3 = 8
			object.priority = PRIORITY_ACTIVE
			object.state = 1
		end if
		break
	case 1
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			object.state = 2
		end if
		break
	case 2
		CallFunction(Eggman_Function138)
		object.xpos += object.xvel
		if object.xvel > 0
			if object.xpos > object.value7
				object.xpos = object.value7
				object.direction ^= FLIP_X
				FlipSign(object.xvel)
				object.value11 = 0
			end if
		else
			if object.xpos < object.value6
				object.xpos = object.value6
				object.direction ^= FLIP_X
				FlipSign(object.xvel)
				object.value11 = 0
			end if
		end if
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			object[-2].value2 = 1
			object.value10 ^= 1
		end if
		object.value12++
		if object.value12 == 32
			object.value12 = 0
			PlaySfx(SfxName[BossSpark], 0)
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.state == 2
				if object[currentPlayer].ypos > object.ypos
					temp0 = object[currentPlayer].xpos
					temp0 -= object.xpos
					Abs(temp0)
					if temp0 < 0x100000
						temp0 = object[currentPlayer].ypos
						temp0 -= object.ypos
						Abs(temp0)
						if temp0 < 0x600000
							object.state = 3
						else
							if object.value11 == 0
								object.value0 = 0
								object.value10 = 0
								object.state = 5
							end if
						end if
					end if
				end if
			end if
		next
		break
	case 3
		object.value10 = 1
		object.ypos += 0x18000
		if object.ypos > object.value9
			object.state = 4
		end if
		object.value12++
		if object.value12 == 32
			object.value12 = 0
			PlaySfx(SfxName[BossSpark], 0)
		end if
		break
	case 4
		object.value10 = 0
		object.ypos -= 0x18000
		if object.ypos < object.value8
			object.ypos = object.value8
			object.value1 = object.value8
			object.state = 2
		end if
		object.value12++
		if object.value12 == 32
			object.value12 = 0
			PlaySfx(SfxName[BossSpark], 0)
		end if
		break
	case 5
		CallFunction(Eggman_Function138)
		object.value0++
		if object.value0 == 1
			object[-2].value2 = 0
			arrayPos0 = object.entityPos
			arrayPos0--
			ResetObjectEntity(arrayPos0, TypeName[Eggman Bomb], 0, object.xpos, object.ypos)
			object[arrayPos0].value1 = object.entityPos
		else
			object[-1].ypos = object.ypos
			object[-1].ypos += object[-1].value2
		end if
		if object.value0 == 80
			object.value0 = 0
			object.value10 = 1
			object[-1].state = 1
			object.state = 6
		end if
		break
	case 6
		CallFunction(Eggman_Function138)
		object.value0++
		if object.value0 == 20
			object.value0 = 0
			object.value10 = 0
			object.value11 = 1
			object[-2].value2 = 1
			object.state = 2
		end if
		break
	case 7
		object.value0++
		if object.value0 == 60
			if object.direction == FLIP_NONE
				object[-2].xvel = -0x10000
			else
				object[-2].xvel = 0x10000
			end if
			object[-2].yvel = 0
			object[-2].state = 1
		end if
		if object.value0 == 120
			if object.direction == FLIP_NONE
				object[+1].xvel = 0x10000
			else
				object[+1].xvel = -0x10000
			end if
			object[+1].yvel = 0
			object[+1].state = 1
		end if
		if object.value0 == 180
			object.value0 = 0
			object.animation = 5
			object.value5 = 0
			object.state++
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object[+3].priority = PRIORITY_ACTIVE
			object[+3].state = 3
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 8
		object.ypos += object.yvel
		object.yvel += 0x1800
		object.value0++
		if object.value0 == 16
			object.yvel = 0
			object.value0 = 0
			object.animationTimer = 0
			object.state++
		end if
		break
	case 9
		if object.value0 < 24
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.value0++
		else
			object.value0 = 0
			object.yvel = 0
			object.value1 = object.ypos
			object.state++
		end if
		break
	case 10
		CallFunction(Eggman_Function138)
		if object.value0 < 8
			object.value0++
		else
			object.value0 = 0
			object.animation = 6
			object.animationTimer = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break
	case 11
		object.xpos += 0x40000
		object.value1 -= 0x4000
		CallFunction(Eggman_Function138)
		if object.outOfBounds == 1
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break
	end switch
	if object.value3 != 0
		if object.value4 > 0
			object.value4--
			GetBit(temp0, object.value4, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value4 == 0
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 24, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
					if checkResult == 1
						object.value3--
						if object.value3 == 0
							player.score += 1000
							object[-2].value2 = 0
							object.value0 = 0
							object.animation = 4
							object.animationTimer = 0
							object.value5 = 1
							object.value1 = object.ypos
							object.state = 7
							if object[-1].type == TypeName[Eggman Bomb]
								temp0 = object.entityPos
								temp0--
								ResetObjectEntity(temp0, TypeName[Explosion], 0, object[-1].xpos, object[-1].ypos)
							end if
						else
							object.animation = 3
							object.animationTimer = 0
							object.value4 = 64
							PlaySfx(SfxName[Boss Hit], 0)
						end if
					end if
				end if
			end if
		next
	end if
	if object.state != 11
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value5 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		if saveRAM[495] == 0
			object.animationTimer++
			object.animationTimer %= 15
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(7, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == 1
			if saveRAM[495] == 0
				LoadSpriteSheet("CNZ/Objects.gif")
			else
				LoadSpriteSheet("CNZ/ObjectsE.gif")
			endif
		SpriteFrame(-28, -16, 44, 16, 76, 140)
		SpriteFrame(-28, -16, 44, 16, 121, 140)
		SpriteFrame(-28, -16, 44, 16, 166, 140)
		SpriteFrame(-28, -16, 44, 16, 211, 140)
		SpriteFrame(-28, -16, 44, 16, 166, 157)
		SpriteFrame(-28, -16, 44, 16, 211, 157)
		SpriteFrame(-40, -40, 80, 72, 175, 183)
		SpriteFrame(-3, -18, 11, 8, 154, 157)
		if options.vsMode == 1
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -16, 44, 16, 1, 5)
		SpriteFrame(-28, -16, 44, 16, 62, 5)
		SpriteFrame(-28, -16, 44, 16, 1, 26)
		SpriteFrame(-28, -16, 44, 16, 62, 26)
		SpriteFrame(-28, -16, 44, 16, 1, 47)
		SpriteFrame(-28, -16, 44, 16, 62, 47)
		SpriteFrame(-40, -40, 80, 72, 232, 112)
		SpriteFrame(-3, -18, 11, 8, 45, 88)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
